﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using tscmcnet;

namespace CS_ThicknessDemo
{
    public class ThicknessController
    {
        //通信类
        TSCMCAPINET protocol;
        int controller_idx;//控制器编号
        int local_port;//本地端口
        string dstip;//目标ip
        int dstport;//目标端口

        public delegate void AddLogHandler(string content, string source = "膜厚");
        public static AddLogHandler AddLogEventHandler;

        DataNode[] dataNode;//数据

        /// <summary>
        /// 膜厚控制器实例
        /// </summary>
        /// <param name="controllerIdx">控制器编号</param>
        /// <param name="localPort">本地端口</param>
        /// <param name="dstIp">目标ip</param>
        /// <param name="dstPort">目标端口</param>
        public ThicknessController(int controllerIdx, int localPort = 8001, string dstIp = "", int dstPort = 8002)
        {
            this.controller_idx = controllerIdx;
            this.local_port = localPort;
            this.dstip = dstIp;
            this.dstport = dstPort;
            protocol = new TSCMCAPINET();
            bool res = SetConfiguration(localPort, dstip, dstport);
            AddLogEventHandler?.Invoke("设置通信类型为以太网");
            if (res)
            {
                AddLogEventHandler?.Invoke("设置参数成功");
            }
            else
            {
                AddLogEventHandler?.Invoke("设置参数失败");
            }
        }



        /// <summary>
        /// 设置配置
        /// </summary>
        /// <param name="localPort">本地端口</param>
        /// <param name="dstIp">目标IP</param>
        /// <param name="dstPort">目标IP</param>
        /// <returns></returns>
        protected bool SetConfiguration(int localPort, string dstIp, int dstPort)
        {
            controller_idx = 0;
            protocol.SetConnectionType(CONNECTION_TYPE.ETHERNET);
            //设置本地网口监听端口
            protocol.SetUdpPort(localPort);
            //设置要连接的下位机IP地址与通信端口
            bool result = protocol.SetDestUdpEndPoint(dstIp, dstPort);
            return result;
        }

        /// <summary>
        /// 打开连接
        /// </summary>
        /// <returns></returns>
        public bool OpenConnection()
        {
            bool result = protocol.OpenConnectionPort();
            if (result)
            {
                AddLogEventHandler?.Invoke("连接通道打开正常");
            }
            else
            {
                AddLogEventHandler?.Invoke("连接通道打开异常");
            }
            return result;
        }

        /// <summary>
        /// 关闭连接
        /// </summary>
        /// <returns></returns>
        public bool CloseConnection()
        {
            AddLogEventHandler?.Invoke("关闭连接通道");
            bool result = protocol.CloseConnectionPort();
            if (result)
            {
                AddLogEventHandler?.Invoke("关闭通道打开正常");
            }
            else
            {
                AddLogEventHandler?.Invoke("关闭通道打开正常");
            }
            return result;
        }

        /// <summary>
        /// 建立连接
        /// </summary>
        /// <returns></returns>
        public bool SetConnectionOn(int controllerIndex)
        {
            AddLogEventHandler?.Invoke("建立连接");
            ERRCODE err = protocol.SetConnectionOn(controllerIndex);
            if (err == ERRCODE.OK)
            {
                AddLogEventHandler?.Invoke("建立连接正常");
                return true;
            }
            else
            {
                protocol.CloseConnectionPort();
                AddLogEventHandler?.Invoke("建立连接异常，正在关闭通道");
                return false;
            }
        }

        /// <summary>
        /// 断开连接
        /// </summary>
        /// <returns></returns>
        public bool SetConnectionOff(int controllerIndex)
        {
            AddLogEventHandler?.Invoke("断开连接");
            ERRCODE err = protocol.SetConnectionOff(controllerIndex);
            if (err == ERRCODE.OK)
            {
                AddLogEventHandler?.Invoke("断开连接正常");
                return true;
            }
            else
            {
                protocol.CloseConnectionPort();
                AddLogEventHandler?.Invoke("断开连接异常，正在关闭通道");
                return false;
            }
        }

        /// <summary>
        /// 开始连续采集
        /// </summary>
        /// <param name="controllerIndex">控制器编号</param>
        /// <param name="dataNodeArray">数据</param>
        /// <returns></returns>
        public ERRCODE SetDataOutputOn(int controllerIndex, ref DataNode[] dataNodeArray)
        {
            ERRCODE err;
            err = protocol.SetDataOutputOn(controller_idx);//开始连续测量
            if (err == ERRCODE.OK)
            {
                AddLogEventHandler?.Invoke("正在开始连续测量");
            }
            else
            {
                AddLogEventHandler?.Invoke("连续测量异常，关闭通道");
                protocol.CloseConnectionPort();
            }
            /*从缓存区中读取数据*/
            Thread.Sleep(100);
            err = protocol.TransferAllDataNode(ref dataNodeArray, dataNodeArray.Length);
            if (err == ERRCODE.OK)
            {
                AddLogEventHandler?.Invoke("数据读取正常");
            }
            else
            {
                AddLogEventHandler?.Invoke("读取异常，关闭通道");
                protocol.CloseConnectionPort();
            }
            return err;
        }

        /// <summary>
        /// 关闭连续采集
        /// </summary>
        /// <param name="controllerIndex">控制器编号</param>
        /// <returns></returns>
        public ERRCODE SetDataOutputOff(int controllerIndex)
        {
            //停止连续测量
            ERRCODE err = protocol.SetDataOutputOff(controllerIndex);
            if (err == ERRCODE.OK)
            {
                AddLogEventHandler?.Invoke("停止连续测量正常");
            }
            else
            {
                AddLogEventHandler?.Invoke("停止连续测量异常，关闭通道");
                protocol.CloseConnectionPort();
            }
            return err;
        }

        /// <summary>
        /// 解析通道数据
        /// </summary>
        /// <param name="sensorChannel">传感器通道</param>
        /// <param name="dataAry">数据</param>
        /// <returns></returns>
        public List<double> ParseChannelValue(int sensorChannel, DataNode[] dataAry)
        {
            List<double> sensorData = new List<double>();
            foreach (var item in dataAry)
            {
                if (item.cfg.channel == sensorChannel)
                {
                    if (item.data > 0)
                    {
                        sensorData.Add(item.data);
                    }
                }
            }
            return sensorData;
        }

    }
}
